<template>
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect @getCategoryId="getCategoryId" :show="scene != 0" />
    </el-card>
    <el-card>
      <!-- 展示spu列表结构 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-bottom: 10px"
          :disabled="!category3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <hint-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              ></hint-button>
              <hint-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              ></hint-button>
              <hint-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu全部sku列表"
                @click="findBySpu(row)"
              ></hint-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="delSpu(row)"
              >
                <hint-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                  slot="reference"
                ></hint-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
          style="text-align: center"
          :current-page="page"
          :page-sizes="[3, 5, 7]"
          :page-size="limit"
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>

      <!-- 添加spu || 修改spu -->
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu" />

      <!-- 添加sku -->
      <SkuForm v-show="scene == 2" ref="sku" @changeScenes="changeScenes" />
    </el-card>

    <el-dialog 
      :title="`${spu.spuName}的sku列表`" 
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table border :data="skuList" style="width: 100%" v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column>
        <el-table-column prop="weight" label="重量" width="width">
        </el-table-column>
        <el-table-column prop="prop" label="默认图片" width="width">
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  components: { SpuForm, SkuForm },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      page: 1,
      limit: 3,
      records: [],
      total: 0,
      //scene：0代表展示spu列表结构  1代表展示添加spu || 修改spu   2代表展示添加sku
      scene: 0,
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true
    };
  },
  methods: {
    //三级联动的自定义事件
    getCategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId;
        //清楚2,3级分类ID
        this.category2Id = "";
        this.category3Id = "";
      } else if (level == 2) {
        this.category2Id = categoryId;
        //清除3级ID
        this.category3Id = "";
      } else {
        this.category3Id = categoryId;
        //获取spu列表数据进行展示
        this.getSpuList();
      }
    },
    async getSpuList() {
      const { page, limit, category3Id } = this;
      const res = await this.$API.spu.reqSpuList(page, limit, category3Id);
      // console.log(res);
      if (res.code === 200) {
        this.total = res.data.total;
        this.records = res.data.records;
      }
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    //添加spu按钮回调
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },
    //修改spu按钮回调
    updateSpu(row) {
      this.scene = 1;
      //获取spuForm子组件
      // console.log(this.$refs.spu);
      this.$refs.spu.initSpuFormData(row);
    },
    //自定义事件（spuForm）
    changeScene({ scene, flag }) {
      // console.log(scene);
      this.scene = scene;
      // this.getSpuList()
      if (flag == "修改") {
        this.getSpuList(this.page);
      } else {
        this.getSpuList((this.page = 1));
      }
    },
    //删除spu按钮事件
    async delSpu(row) {
      let res = await this.$API.spu.reqDeleteSpu(row.id);
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.getSpuList();
      }
    },
    //添加sku按钮事件
    addSku(row) {
      this.scene = 2;
      //
      this.$refs.sku.getSkuData(this.category1Id, this.category2Id, row);
    },
    changeScenes(scene) {
      this.scene = scene;
    },
    //查看当前spu全部sku列表
    async findBySpu(row) {
      this.dialogTableVisible = true;
      this.spu = row;
      let res = await this.$API.spu.reqSkuList(row.id);
      // console.log(res);
      if (res.code == 200) {
        this.skuList = res.data;
        this.loading = false
      }
    },
    close(done) {
      this.loading = true
      this.skuList = []
      done()
    }
  },
};
</script>

<style lang="scss" scoped>
</style>